package com.tkjt.pms.common.vos;

import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

public class RequestJson {
    private final static int defaultPageNo=1; //默认页码
    private final static int defaultPageSize=10;//默认每页数

    private JSONObject jsonObject;

    public JSONObject getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(JSONObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    public Object get(String key) {
        return this.jsonObject.get(key);
    }
    public RequestJson(JSONObject jsonObject){
        this.jsonObject=jsonObject;
    }
    public RequestJson(){
        this.jsonObject=new JSONObject();
    }
    public boolean getBoolean(String key) {
        Object o = this.get(key);
        if (o != null) {
            if (o.equals(Boolean.FALSE) || o instanceof String && ((String)o).equalsIgnoreCase("false")) {
                return false;
            }

            if (o.equals(Boolean.TRUE) || o instanceof String && ((String)o).equalsIgnoreCase("true")) {
                return true;
            }
        }

        return false;
    }

    public double getDouble(String key) {
        Object o = this.get(key);
        if (o != null) {
            try {
                return o instanceof Number ? ((Number)o).doubleValue() : Double.parseDouble((String)o);
            } catch (Exception var4) {
                return 0.0;
            }
        } else {
            return 0.0;
        }
    }

    public int getInt(String key) {
        Object o = this.get(key);
        if (o != null) {
            return o instanceof Number ? ((Number)o).intValue() : (int)this.getDouble(key);
        } else {
            return 0;
        }
    }

    public JSONArray getJSONArray(String key) {
        Object o = this.get(key);
        if (o != null && o instanceof JSONArray) {
            return (JSONArray)o;
        } else {
            return null;
        }
    }

    public JSONObject getJSONObject(String key) {
        Object o = this.get(key);
        if (JSONNull.getInstance().equals(o)) {
            return new JSONObject(true);
        } else if (o instanceof JSONObject) {
            return (JSONObject)o;
        } else {
            return null;
        }
    }

    public long getLong(String key) {
        Object o = this.get(key);
        if (o != null) {
            return o instanceof Number ? ((Number)o).longValue() : (long)this.getDouble(key);
        } else {
            return 0l;
        }
    }

    public String getString(String key) {
        Object o = this.get(key);
        if (o != null) {
            return o.toString();
        } else {
            return null;
        }
    }

    public int getPageNo(){
        Object pageNo = this.jsonObject.get("pageNo");
        if(pageNo == null){
            return defaultPageNo;
        }else{
            return (Integer)pageNo;
        }
    }

    public int getPageSize(){
        Object pageSize = this.jsonObject.get("pageSize");
        if(pageSize == null){
            return defaultPageSize;
        }else{
            return (Integer)pageSize;
        }
    }
}
